package proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @description: TODO 类描述
 * @author: kanggw
 * @date: 2023/1/12
 **/
public class MyInvocationHandler implements InvocationHandler {

    private Object target;

    public MyInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("调用前操作。名称:" + method.getName());

        Object invoke = method.invoke(target, args);
        System.out.println("调用后操作");
        return invoke;
    }

    public static void main(String[] args) {
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");

        Helloworld helloworld = (Helloworld) Proxy.newProxyInstance(MyInvocationHandler.class.getClassLoader(),
                new Class<?>[]{Helloworld.class},
                new MyInvocationHandler(new HelloworldImpl()));

        helloworld.sayHello();
    }
}
